@using FOPS.Abstract.MetaInfo.Server
@using FOPS.Abstract.Builder.Enum
@using FOPS.Abstract.Builder.Server
@using FOPS.Abstract.MetaInfo.Entity
@using FOPS.Abstract.Builder.Entity
@using System.Timers
@using FS.Extends

@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;
@implements IDisposable;

<div class="layui-table-header">
    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
        <thead>
        <tr>
            <th data-field="ID">
                <div class="layui-table-cell laytable-cell-build-0">
                    <span>ID</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-build-1">
                    <span>项目名称</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-build-2">
                    <span>状态</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-build-3">
                    <span>操作</span>
                </div>
            </th>
        </tr>
        </thead>
    </table>
</div>
<div class="layui-table-body layui-table-main">
    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
        <tbody>
        @for (var index = 0; index < _lstBuild.Count; index++)
        {
            var info = _lstBuild[index];
            <tr data-index="@index">
                <td>
                    <div class="layui-table-cell laytable-cell-build-0"> @info.Id</div>
                </td>
                <td>
                    <div class="laytable-cell-build-1">
                        @{
                            var projectVO = _lst.Find(o => o.Id == info.ProjectId);
                            if (projectVO == null) projectVO = _iocManager.Resolve<IProjectService>().ToInfo(info.ProjectId) ?? new ProjectVO();
                        }
                        <a href="/builder/log/@info.Id">@projectVO.Name</a>
                    </div>
                </td>
                <td>
                    <div class="layui-table-cell laytable-cell-build-2">
                        @switch (info.Status)
                        {
                            case EumBuildStatus.None:
                                <button class="layui-btn layui-btn-xs layui-btn-primary">未开始</button>
                                break;
                            case EumBuildStatus.Building:
                                <button class="layui-btn layui-btn-xs layui-btn-normal">构建中</button>
                                break;
                            case EumBuildStatus.Finish:
                                if (info.IsSuccess)
                                {
                                    <button class="layui-btn layui-btn-xs">成功</button>
                                }
                                else
                                {
                                    <button class="layui-btn layui-btn-xs layui-btn-danger">失败</button>
                                }
                                break;
                        }
                    </div>
                </td>
                <td>
                    <div class="layui-table-cell laytable-cell-build-3">
                        @if (info.Status == EumBuildStatus.None)
                        {
                            <button class="layui-btn layui-btn-radius layui-btn-xs" @onclick="() => CancelBuild(info)">
                                <i class="layui-icon"></i>
                            </button>
                        }
                        else if (info.Status == EumBuildStatus.Building)
                        {
                            <span>@((DateTime.Now - info.CreateAt).GetDateDesc())</span>
                            <button class="layui-btn layui-btn-radius layui-btn-xs" @onclick="() => CancelBuild(info)">
                                <i class="layui-icon"></i>
                            </button>
                        }
                        else
                        {
                            <span lay-tips="耗时：@((info.FinishAt - info.CreateAt).GetDateDesc())">@((DateTime.Now - info.FinishAt).GetDateDesc())</span>
                        }
                    </div>
                </td>
            </tr>
        }
        </tbody>
    </table>
</div>

<style>
    .laytable-cell-build-0{ width: 60px; }
    .laytable-cell-build-1{ width: 150px; }
    .laytable-cell-build-2{ width: 80px; }
    .laytable-cell-build-3{ width: 130px; }
</style>

@code{
    private List<BuildVO> _lstBuild = new();
    private List<ProjectVO> _lst = new();
    private Timer _timer;
    private bool _isFirst;

    private async Task CancelBuild(BuildVO info)
    {
        await _iocManager.Resolve<IBuildService>().Cancel(info.Id);
        await GetList();
    }

    protected override async Task OnInitializedAsync()
    {
        if (_isFirst) return;
        _isFirst = true;

        _lst = await _iocManager.Resolve<IProjectService>().ToListAsync();
        await GetList();
    }

    private async Task GetList()
    {
        var pageIndex = 1;
    // 重新读取所有列表
        _lstBuild = await _iocManager.Resolve<IBuildService>().ToBuildingList(20, pageIndex);

    // 说明本次构建的队列大于20条，需要都读出来
        while (_lstBuild.Count > 0 && _lstBuild.All(o => o.Status == EumBuildStatus.None))
        {
            pageIndex++;
            _lstBuild.AddRange(await _iocManager.Resolve<IBuildService>().ToBuildingList(20, pageIndex));
        }
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            _timer = new Timer(2000);
            _timer.Elapsed += async (_, _) =>
            {
                try
                {
                    await GetList();
    // 刷新页面
                    await InvokeAsync(StateHasChanged);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            };
            _timer.Enabled = true;
        }

        return Task.FromResult(0);
    }

    public void Dispose()
    {
        _timer?.Dispose();
    }
}